<?php


namespace app\controller\Web;

use support\Request;
use Throwable;
use app\service\Web\NewsService;
use app\util\ResponseTrait;

use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("新闻")
 */
class NewsController
{
    use ResponseTrait;

    /**
     * @Apidoc\Url("/web/news/getList")
     * @Apidoc\Title("列表")
     * @Apidoc\Method("GET")
     * @Apidoc\Query(ref="pagingParam")
     * @Apidoc\Query("lang",type="string",require=false,default="",desc="语言类型【需写接口】")
     * @Apidoc\Query("news_cate_id",type="int",require=false,default="",desc="新闻分类ID")
     * @Apidoc\Query("platform",type="string",require=false,default="",desc="平台类型【需写接口】")
     * @Apidoc\Returned(ref="pagingResponse")
     * @Apidoc\Returned("list",type="array", require=false, default="[]",desc="新闻列表",table="news",children={
     *      @Apidoc\Returned("news_cate",type="object", require=false, default="{}",desc="新闻分类",table="news_cate",field="id,name")
     * })
     */
    public function getList(Request $request){
        try {
            $where = [];
            $page = parameterCheck($request->input('page'), 'int', 1);
            $pageSize = parameterCheck($request->input('pageSize'), 'int', 10);

            $where['is_show'] = 10;
            $where['lang'] = parameterCheck($request->input('lang'), 'string', '');
            $where['news_cate_id'] = parameterCheck($request->input('news_cate_id'), 'float', 0);
            $where['platform'] = parameterCheck($request->input('platform'), 'string', '');

            $data = NewsService::getList($where, $page, $pageSize);

            return $this->success($data);
        } catch (Throwable $e) {
            return $this->fail($e);
        }
    }

    /**
     * @Apidoc\Url("/web/news/getInfo")
     * @Apidoc\Title("详情")
     * @Apidoc\Method("GET")
     * @Apidoc\Query("id",type="int",require=true,default="1",desc="新闻ID")
     * @Apidoc\Query("news_cate_id",type="int",require=false,default="",desc="新闻分类ID")
     * @Apidoc\ResponseSuccess("data",type="object", table="news",children={
     *     @Apidoc\Returned("prev_id",type="int", require=false, default="",desc="上一条新闻ID"),
     *     @Apidoc\Returned("prev_title",type="int", require=false, default="",desc="上一条新闻标题"),
     *     @Apidoc\Returned("next_id",type="int", require=false, default="",desc="下一条新闻ID"),
     *     @Apidoc\Returned("next_title",type="int", require=false, default="",desc="下一条新闻标题")
     * })
     */
    public function getInfo(Request $request)
    {
        try {
            $where = [];

            $id = parameterCheck($request->input('id'), 'int', 0);
            $where['news_cate_id'] = parameterCheck($request->input('news_cate_id'), 'float', 0);

            $data = NewsService::getInfo($id,$where);

            return $this->success($data);
        } catch (Throwable $e) {
            return $this->fail($e);
        }
    }

    /**
     * 生成静态页
     */
    public function genHtml(){


        $id = 2;
        $info = NewsService::getOne($id);

        $template_file_path = public_path().'/template/news.html';//模版文件路径

        $static_path = public_path().'/pc/news/';
        if(!is_dir($static_path)){
            mkdir(iconv('UTF-8', 'GBK', $static_path),0777,true);
        }
        $static_file_path = $static_path.$id.'.html';//生成静态文件路径


        $fp = fopen($template_file_path,'r'); //只读打开模板
        $str = fread($fp,filesize($template_file_path));//读取模板中内容
        $str = str_replace('{title}',$info['title'],$str);
        $str = str_replace('{content}',$info['content'],$str);//替换内容
        fclose($fp);

        $handle = fopen($static_file_path,'w'); //写入方式打开新闻路径
        fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件
        fclose($handle);

        return $this->success();
    }
}